﻿
#ifndef _SCENE_LOGIC_H_
#define _SCENE_LOGIC_H_

#include <unordered_map>
#include "game_msg.h"
#include "../share/common_singleton.h"
#include "../share/jsonmessage.h"
#include "Player.h"
#include "../share/proxy_head.h"
#include "../share/object.h"
#include "../share/timer.h"



class CPlayerLoopTimer : public common::CTimer
{
public:
	virtual void TimerActive();
};


typedef std::unordered_map< int, OBJ_ID_TYPE > TObjIDMap;
typedef std::unordered_map< int, int > TPlayerIDMapIDMap;
typedef std::vector< int > TPlayerDestroyVector;

class CSceneLogic :public common::CSingleton<CSceneLogic>
{
public:
	CSceneLogic(){}
	~CSceneLogic(){}
public:
	static void ServiceExitting();
	void TickUpdate();
	static bool PlayerUpdate(::common::CObj& obj, void* param);
public:
	void SetPlayerSomeInfoToRedisDBByPlayerId(int32_t playerid, std::string field, std::string value, uint32_t bTable);
	void SetPlayerSomeInfoToRedisDBByPlayerId(int32_t playerid, std::string field, int32_t value);
public:
	void Initialize();
	bool LoadGameData();
	// 保存游戏数据
	bool SaveGameData ();
	void SaveSurpriseRechargeData ();

	//检查给玩家补发惊喜充值邮件
	void CheckSendEmail4SurpriseRecharge ();
	Player* GetPlayerByPlayerID(int playerId);
	Player* GetPlayerByObjID(OBJ_ID_TYPE obj_id);
	Player* GetPlayerBySocket(int socket);
	int32_t GetMapIdByPlayerId(int32_t playerId);
	int32_t GetOnlineCount() { return player_id_map_.size(); }
	TObjIDMap GetPlayerIdMap() { return player_id_map_; }
	
	void KickPlayer(Player& player);
	void OnPlayerDisconnect(int32_t fd);
	void CloseTcpConnect(int32_t fd);
	int PlayerLeaveGame(Player& player);
	int PlayerEnterGame(Player& player);
	void AddPlayerDestroy(Player& player);
	int RegistePlayerSocket(int32_t fd_, int obj_id);
	int UnRegistePlayerSocket(int32_t fd_);
	void SetPlayerInfoToCpp(int32_t playerId, std::string& playerinfo);

	void SendMessageToOnlinePlayer(std::string * notify, uint32_t msgId);
	void SendGmMessageToOnlinePlayer(uint32_t tp, std::string msg, uint32_t ct);
	void SendGmMarqueeToOnlinePlayer(uint32_t spn, uint32_t tp, std::string msg, uint32_t priority = 1);
	// lcl 2017-11-8 定时更新所有玩家
	void update_time_to_online_player(unsigned int label_id);

	void GetRecommendFriend(uint32_t playerid, const std::unordered_map<uint32_t, int32_t>& friend_map, std::set<uint32_t> recommend_list);
	void SetPlayerId2MidMap(uint32_t playerId, uint32_t mapid);

	void FillOnlinePlayer(int32_t activityid, int32_t max_times, int32_t count, int32_t minlv, int32_t maxlv, Json::Value& online_list, int32_t start);

	void PlayerDestroyDoing();
	const TObjIDMap& GetOnlineSocket();
private:
	TObjIDMap player_id_map_;
	TObjIDMap socket_id_map_;
	TPlayerIDMapIDMap m_pid2mapid;
	TPlayerDestroyVector player_destroy_;

	int32_t last_player_tick_;

	CPlayerLoopTimer save_timer_;
};

#endif